package ro.flashbite.helpers
{
	import ro.flashbite.errors.ArgumentIncompatibleError;
	import ro.flashbite.errors.DontInstanciateError;
	
	import flash.display.DisplayObject;
	import flash.net.registerClassAlias;
	import flash.system.Capabilities;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;

	/**
	 * Static helpers uncategorized
	 * 
	 * v 1.0
	 * 
	 * @author Adrian Barbu
	 */	
	public final class GlobalHelpers
	{
		public function GlobalHelpers() { throw new DontInstanciateError(); }
		
		/**
		 * Determine if the SWF is running in the StandAlone player
		 *  
		 * @return : (Boolean) true if standalone, false if external
		 * 
		 */		
		static public function isStandalone():Boolean
		{
			return (Capabilities.playerType == "StandAlone");
		}
		
		/**
		 * Determine if the SWF is running in a browser plug-in
		 * 
		 * @return : (Boolean) true if is playing in the plugin, false if activeX
		 */
		public static function isPlugin():Boolean 
		{
			return (Capabilities.playerType == "PlugIn");
		}
		
		/**
		 * Gets a displayObject location
		 *  
		 * @param srcDO : (DisplayObject) item to be checked
		 * @return : (Boolean) true if is locale file, false otherwise
		 */		
		static public function isLocal(srcDO:DisplayObject):Boolean
		{
			if (srcDO == null){
				throw new ArgumentIncompatibleError("srcDO");
			}
			
			return srcDO.loaderInfo.url.indexOf("file:///") == 0;
		}
		
		/**
		 * Determines if textfield has more text than can be displayed at once
		 * 
		 * @param textField : (Textfield) textField to check
		 * @return : (Boolean) true if has overflow (in this case we can attach for example a UIScrollBar), false otherwise
		 * 
		 */		
		static public function textFieldHasOverFlow(tf:TextField):Boolean
		{
			if (tf == null){
				throw new ArgumentIncompatibleError("tf");
			}
			
			return tf.maxScrollV > 1;
		}
		
		/**
		 * Create a clone of the object; In many cases, this function doesn't work because flash limitations
		 * 
		 * @param source : (* [Object]) object used to be cloned
		 * @return : (* [Object]) the cloned object
		 */		
		static public function objectClone(source:*):*
		{
			if (source == null) 
				throw new ArgumentError("Source object is null!!!");
			
			var className	: String 	= getQualifiedClassName(source),
				ba			: ByteArray = new ByteArray();
			
			className = className.replace("::", ".");
			registerClassAlias(className, source.constructor as Class);
			
			ba.writeObject(source);
			ba.position = 0;
			return ba.readObject();
		}
	}
}